{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "# 本周知识\n",
    "> 1.函数的返回值\n",
    "\n",
    "> 2.函数的参数类型\n",
    "\n",
    "> 3.模块与函数的关系\n",
    "\n",
    "> 4.定义一个模块，然后调用模块\n",
    "\n",
    ">> A.调用模块\n",
    ">> B.调用模块中的函数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 函数的返回值\n",
    "> 1.返回的关键字：return\n",
    "\n",
    "> 2.在python中return可以不写，return默认为None\n",
    "\n",
    ">> 1.编程语言中的函数，一定有返回值\n",
    "\n",
    ">> 2.返回值的结果是一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    full_name = first_name+''+last_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Huruihan\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Hu\",\"ruihan\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.函数的参数\n",
    "### 1.函数输入的参数\n",
    "> 1. 函数是可以输入复杂的参数的\n",
    ">> 1.函数参数为列表\n",
    ">> 2.函数参数为字典\n",
    "> 2.传递任意数量的实参\n",
    ">> 1.*args\n",
    ">> 2.*kwrgs\n",
    "\n",
    "\n",
    "### 2.函数返回的结果\n",
    "> 1.函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构\n",
    ">> A. 函数返回一个集合\n",
    ">> B. 函数返回一个字典 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    \n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Huruihan', 'peiqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Huruihan\",\"peiqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \n",
    "    \n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    \n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad737f-7f5950b167a70a144d4c082b\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.236\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61ad737f-7f5950b167a70a144d4c082b'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 2.传递任意数量的实参\n",
    ">> 1.*args\n",
    ">>> 1.args可以接受多个数据（单值）\n",
    ">>> 2.args将多个数据存在一个元组当中，实际还是一个变量\n",
    "\n",
    ">> 2.*kwrgs\n",
    ">>> 1.kwargs 可以接受多个数据（键值对）=>关键自参数\n",
    ">>> 2.kwargs 将多个键值对的数据存放在一个字典中，实际上还是一个变量 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# *args 实践\n",
    "def make_food(food,*toppings):\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    " build_food = make_food(\"pizza\",\"mushroom\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom',))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " build_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# **kwargs 实践\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人的信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'dict' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-35-8a0f24557104>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbuild_person\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mperson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Lihua\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m18\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mjob\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"student\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0maddress\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"广州南方学院\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0me_mail\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"123456@qq.com\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'dict' object is not callable"
     ]
    }
   ],
   "source": [
    "build_person = person(\"Lihua\",age=18,job=\"student\",address=\"广州南方学院\",e_mail=\"123456@qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function build_person at 0x0000018BB673D940>\n"
     ]
    }
   ],
   "source": [
    "print(build_person)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.函数返回的结果\n",
    "> 1.函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构\n",
    ">> A. 函数返回一个集合\n",
    ">> B. 函数返回一个字典 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个集合\n",
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a', 'i', 'o', 'u', 'e'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels('hello Ruihan')\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个字典\n",
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Lihua', 'age': 18, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person(\"Lihua\",18,\"student\")\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import study\n",
    "study.search4vowels('hello ruihan')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from study import news_链接 #该方法不需要再写模块的名字+.func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "news_链接('https://www.nfu.edu.cn/','xxyw','98')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'study' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-40-05218a22606e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'https://www.nfu.edu.cn/'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'xxyw'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'98'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'study' is not defined"
     ]
    }
   ],
   "source": [
    "result = study.news_链接('https://www.nfu.edu.cn/','xxyw','98')\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
